package com.ym.medical.service.impl;

import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.dao.UserTaglibraryDao;
import com.ym.medical.domain.entity.CustomTaglibraryEntity;
import com.ym.medical.domain.entity.StandarTaglibraryEntity;
import com.ym.medical.domain.entity.UserTaglibraryEntity;
import com.ym.medical.domain.vo.UserTaglibraryVo;
import com.ym.medical.enums.UserTaglibraryTagTypeEnum;
import com.ym.medical.service.ICustomTaglibrarySV;
import com.ym.medical.service.IStandarTaglibrarySV;
import com.ym.medical.service.IUserTaglibrarySV;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
                                                        
/**
* 用户标签表Service业务层处理
*
* @author ym
* @date 2020-08-20
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class UserTaglibrarySVImpl extends BaseSVImpl<UserTaglibraryEntity,UserTaglibraryDao> implements IUserTaglibrarySV {

	@Autowired
	private IStandarTaglibrarySV standarTaglibrarySV;

	@Autowired
	private ICustomTaglibrarySV customTaglibrarySV;

	@Override
	public List<UserTaglibraryEntity> selectByOrderId(Long ioId, Long doctorId, Long userId) {
		return this.selectList(UserTaglibraryEntity.builder().orderId(ioId).tagedUserId(doctorId).tagUserId(userId).build());
	}

	@Override
	public List<UserTaglibraryVo> selectModelByIoId(Long orderId) {
		List<UserTaglibraryVo> userTaglibraryModels = new ArrayList<>();
		List<UserTaglibraryEntity> userTaglibraries = selectByOrderId(orderId,null,null);
		if(!userTaglibraries.isEmpty()){
			for (UserTaglibraryEntity userTaglibrary : userTaglibraries) {
				UserTaglibraryVo userTaglibraryModel = new UserTaglibraryVo();
				BeanUtils.copyProperties(userTaglibrary,userTaglibraryModel);
				String tagType = userTaglibrary.getTagType();
				Long tagId = userTaglibrary.getTagId();
				String tglbName = null;
				if(UserTaglibraryTagTypeEnum.STANDARD_TAG.getKey().equals(tagType)){
					StandarTaglibraryEntity standarTaglibrary = standarTaglibrarySV.selectById(tagId);
					if(standarTaglibrary != null){
						tglbName =standarTaglibrary.getTglbName();
					}
				}else {
					CustomTaglibraryEntity customTaglibrary = customTaglibrarySV.selectById(tagId);
					if(customTaglibrary != null){
						tglbName =customTaglibrary.getTglbName();
					}
				}
				userTaglibraryModel.setTglbName(tglbName);
				userTaglibraryModels.add(userTaglibraryModel);
			}
		}
		return userTaglibraryModels;
	}
}
